Skip to content

ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 #29245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

immaxchen
Copy link
Contributor

For vmin vmax use the same implementation as Styler.bar
For dtype Int64 issue, deprecated .values and use .to_numpy instead
Here explicitly assign the dtype to float since we are doing normalize

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
@@ -958,6 +958,8 @@ def background_gradient(
axis=0,
subset=None,
text_color_threshold=0.408,
vmin=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you annotate the new parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review @WillAyd, but I don't quite get it what do you mean by "annotate", can you elaborate a little more? (I have docstring already so I suppose you don't mean docstring right?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it! you mean the type-hints annotation right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! 😄

smin = s.values.min()
smax = s.values.max()
smin = s.min() if vmin is None else vmin
if isinstance(smin, ABCSeries):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not already covered by the line directly preceding it?

Copy link
Contributor Author

@immaxchen immaxchen Oct 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here depends on the axis argument provided to Styler.background_gradient, s can be a DataFrame or a Series. if s is a DataFrame and vmin not provided, smin will first become a Series then become the min value of the full data, the code here is consistent with (aka. direct copy from) Styler.bar method, do we want to change it?

Copy link
Contributor Author

@immaxchen immaxchen Oct 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note: try to use s.values.min() to get overall minima will produce AttributeError: 'IntegerArray' object has no attribute 'min' as described in #28869. OTOH, use np.min(s.values) will also produce NotImplementedError: The 'reduce' method is not supported. I thought IntegerArray might be a WIP and .min().min() is the workaround.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this into a helper function? I think this affects highlight_extrema as well but seems to tackle slightly differently; should keep unified

squeeze might also be more appropriate to use here

Copy link
Contributor Author

@immaxchen immaxchen Nov 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! didn't notice the highlight_extrema, they really should be consistent. turned-out a fairly simple approach can be used: np.nanmin(s.to_numpy()), s can be any shape

@WillAyd WillAyd added the ExtensionArray Extending pandas with custom dtypes or arrays. label Oct 28, 2019
rng = smax - smin
# extend lower / upper bounds, compresses color range
norm = colors.Normalize(smin - (rng * low), smax + (rng * high))
# matplotlib colors.Normalize modifies inplace?
# https://github.com/matplotlib/matplotlib/issues/5427
rgbas = plt.cm.get_cmap(cmap)(norm(s.values))
rgbas = plt.cm.get_cmap(cmap)(norm(s.to_numpy(dtype=float)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK with this but can just use default constructor without dtype argument I think? As it it seems like this is trying to coerce, which I'm not sure why that would be needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, .to_numpy() alone will produce dtype=object and rejected by matplotlib.
OTOH, it will immediately convert to float anyway since cmap require normalizing to [0,1], nothing to loose for coercing in the first place.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger TomAugspurger merged commit 63b75f3 into pandas-dev:master Nov 3, 2019
@TomAugspurger
Copy link
Contributor

Thanks @immaxchen!

Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

background_gradient vmin and vmax
3 participants